﻿using EFDemo.ContextDemo.Models;
using Microsoft.EntityFrameworkCore;
namespace EFDemo.ContextDemo
{
    public class EquipmentContext : DbContext
    {
        private readonly String _connStr;

        public DbSet<Computer> Computers { get; set; }

        public EquipmentContext(IConfiguration configuration)
        {
            _connStr=configuration.GetConnectionString("EquipmentConnectionString");
        }

        public EquipmentContext(DbContextOptions<EquipmentContext> options,IConfiguration configuration):base(options)
        {
            // aspnetcore依赖注入时候调用的构造函数
            _connStr = configuration.GetConnectionString("EquipmentConnectionString");
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer(_connStr);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }
    }
}
